package com.jinkeen.xiaxie.loading

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.transition.TransitionSet
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.Interpolator
import androidx.core.animation.addListener
import com.jinkeen.xiaxie.R
import com.jinkeen.xiaxie.databinding.ActivityLoadingBinding

class LoadingActivity : AppCompatActivity() {
    private lateinit var binding: ActivityLoadingBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoadingBinding.inflate(layoutInflater)
        setContentView(binding.root)
        textFallDown()
//        fallDown()
        binding.shapeLoadingView.setImageResource(R.mipmap.ic_launcher_round)
    }

    fun fallDown() {

        var mFreeFallAnimatiorSet = AnimatorSet()
        var fallAnimator =
            ObjectAnimator.ofFloat(binding.shapeLoadingView, "translationY", 0f, 600f)
        fallAnimator.interpolator = AccelerateInterpolator()
        var shadowAnimtor = ObjectAnimator.ofFloat(binding.indication, "scaleX", 1f, 0.2f)
        mFreeFallAnimatiorSet.setDuration(1000L)
        mFreeFallAnimatiorSet.playTogether(fallAnimator, shadowAnimtor)

        mFreeFallAnimatiorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                binding.shapeLoadingView.setImageResource(R.mipmap.ic_launcher_round)
                up()
            }
        })
        mFreeFallAnimatiorSet.start()
    }

    fun textFallDown() {
        var mFreeFallAnimatiorSet = AnimatorSet()
        var fallAnimator = ObjectAnimator.ofFloat(binding.promptTV, "translationY", 0f, 600f)
        fallAnimator.interpolator = AccelerateInterpolator()
        mFreeFallAnimatiorSet.setDuration(1000L)
        var shadowAnimtor = ObjectAnimator.ofFloat(binding.indication, "scaleX", 1f, 0.2f)
        mFreeFallAnimatiorSet.playTogether(fallAnimator)
        mFreeFallAnimatiorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                textUp()
            }
        })
        mFreeFallAnimatiorSet.start()
    }

    fun up() {

        var mFreeFallAnimatiorSet = AnimatorSet()
        var fallAnimator =
            ObjectAnimator.ofFloat(binding.shapeLoadingView, "translationY", 600f, 0f)
        fallAnimator.interpolator = AccelerateInterpolator()
        var shadowAnimtor = ObjectAnimator.ofFloat(binding.indication, "scaleX", 0.2f, 1f)
        mFreeFallAnimatiorSet.setDuration(1000L)
        mFreeFallAnimatiorSet.playTogether(fallAnimator, shadowAnimtor)

        mFreeFallAnimatiorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                binding.shapeLoadingView.setImageResource(R.mipmap.ic_launcher_round)
                fallDown()
            }
        })
        mFreeFallAnimatiorSet.start()
    }

    fun textUp() {
        var mFreeFallAnimatiorSet = AnimatorSet()
        var fallAnimator = ObjectAnimator.ofFloat(binding.promptTV, "translationY", 600f, 0f)
        fallAnimator.interpolator = AccelerateInterpolator()
        mFreeFallAnimatiorSet.setDuration(1000L)
        var shadowAnimtor = ObjectAnimator.ofFloat(binding.indication, "scaleX", 1f, 0.2f)
        mFreeFallAnimatiorSet.playTogether(fallAnimator)
        mFreeFallAnimatiorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                binding.promptTV.visibility = View.GONE
            }
        })
        mFreeFallAnimatiorSet.start()
    }
}